<?php
    $titlePage = @$this->enterprise->company_name_vn.': '.$this->translator('b2b_gallery_list').' '.$this->set->title;
    $this->headTitle(@$titlePage);

    $this->headLink()
    ->appendStylesheet($this->APP_STATIC_SERVER.'/js/jquery.ui/themes/base/ui.core.css')
    ->appendStylesheet($this->APP_STATIC_SERVER.'/js/jquery.ui/themes/base/ui.resizable.css')
    ->appendStylesheet($this->APP_STATIC_SERVER.'/js/jquery.ui/themes/base/ui.dialog.css')
    ->appendStylesheet($this->APP_STATIC_SERVER.'/js/jquery.ui/themes/base/ui.theme.css');

$this->headScript()
    ->appendFile($this->APP_STATIC_SERVER.'/js/jquery.ui/ui.core.js')
    ->appendFile($this->APP_STATIC_SERVER.'/js/jquery.ui/ui.draggable.js')
	->appendFile($this->APP_STATIC_SERVER.'/js/jquery.ui/ui.droppable.js')
	->appendFile($this->APP_STATIC_SERVER.'/js/jquery.ui/ui.resizable.js')
    ->appendFile($this->APP_STATIC_SERVER.'/js/jquery.ui/ui.dialog.js');
?>

<style>
    .b_a_right{
        text-align: center;
    }
    .g_content{

    }
    .col_20{
        float: left;
        width: 20%;
    }
    .g_content{
        padding : 10px 0 0 0;
    }
    .g_content .g_image{
        text-align: center;
    }
    .g_content .subcolumns{
        padding: 0 0 10px 0;
    }
    .b_a_right span{
        padding: 0 0 0 5px;
    }
    .b_bottom{
        margin-bottom: 10px;
    }
    #Mark{
    
    }
</style>
<?php
    $skin_url = $this->APP_STATIC_SERVER . '/skins/'. $this->APP_TEMPLATE . '/'. $this->APP_SKIN;
?>
<div class="grid_12">
    <div class="grid_8 alpha">
        <?php if(!$this->found):?>
        <?php echo $this->translator('not_found_database');?>
        <?php else:?>
        <!--Sub menu-->
        <?php echo $this->widget('bizprofile','menu',array()); ?>
        <!--End sub menu-->
        <div class="subcolumns pb10 ">
            <div class="b_breadcrumb">
                <!--form edit-->
                <?php
                    $arrParamGallery = array(
                        'enterprise_id' => $this->enterprise->id,
                        'enterprise_name' => Tomato_Utility_String::removeSign($this->enterprise->company_name_en, '-', true)
                    );
                ?>
                <div style="width:70%; float:left; display:none;" id="hd_form">
                    <?php if($this->allow('edit-album') && $this->allowAction):?>
                    <form action="<?php echo $this->url(array(), 'b2b_gallery_album_edit');?>" method="post" id="formEditAlbum" onkeypress="return event.keyCode!=13">
                        <a href="<?php echo $this->url($arrParamGallery, 'b2b_gallery_list');?>"><?php echo $this->translator('gallery_list_breadcrumb');?></a> »
                        <input type="hidden" value="<?php echo $this->set->set_id;?>" name="set_id" />
                        <input type="text" value="<?php echo $this->set->title;?>" name="set_title" id="set_title"/>
                        <img id="btn_unedit" title="<?php echo $this->translator('gallery_edit_album');?>" style="cursor:pointer" src="<?php echo $skin_url;?>/images/edit.png" />
                    </form>
                    <?php endif;?>
                    &nbsp;
                </div>
                <div style="width:70%; float:left;" id="div_unform">
                    <a href="<?php echo $this->url($arrParamGallery, 'b2b_gallery_list');?>"><?php echo $this->translator('gallery_list_breadcrumb');?></a> »
                    <label id="lb_title"><?php echo $this->set->title;?></label>
                    <?php if($this->allow('edit-album') && $this->allowAction):?>
                    <img class="btn_edit" title="<?php echo $this->translator('gallery_edit_album');?>" style="cursor:pointer" src="<?php echo $skin_url;?>/images/edit.png" />
                    <?php endif;?>
                </div>
                <!--end form edit-->

                <div style="width:30%; float:left; text-align:right;">
                    <?php if($this->allow('add') && $this->allowAction):?>
                    <?php
                        $arrRouteAdd = array(
                            'enterprise_id' => $this->enterprise->id,
                            'enterprise_name' => Tomato_Utility_String::removeSign($this->enterprise->company_name_en, '-', true)
                        );
                    ?>
                    <!-- Link "T?i ?nh m?i" -->
                    <a href="<?php echo $this->url($arrRouteAdd, 'b2b_gallery_add');?>"><?php echo $this->translator('gallery_add_btn');?></a>&nbsp;&nbsp;
                    <?php endif;?>
                    <?php if($this->allow('edit-album') && $this->allowAction):?>
                    <a class="btn_edit" href="javascript:void(0)"><?php echo $this->translator('gallery_edit_album');?></a>&nbsp;&nbsp;
                    <?php endif;?>
                    <?php if($this->allow('delete-album') && $this->allowAction):?>
                    <a id="btn_del_album" href="javascript:void(0)"><?php echo $this->translator('gallery_delete_album');?></a>
                    <?php endif;?>
                </div>
            </div>
            <?php if(!$this->numPhoto):?>
            <div style="padding-top:10px; text-align:center;">
                <?php echo $this->translator('gallery_album_not_found');?>
            </div>
            <?php else:?>
            <!--div class="b_a_right">
                &nbsp;<?php //echo $this->paginator()->slide($this->paginator, $this->paginatorOptions);?>
            </div-->
            <div class="g_content" style="margin-top: 10px;">
                <?php $p = $this->offset+1; $i = 0; foreach($this->photos as $photo):?>
                <?php

                    /**
                    * Check exist image
                    **/
                    $file    = $photo->image_thumbnail;
                    $path    = str_replace($this->APP_STATIC_SERVER,getcwd(),$file);
                    $image = (is_file($path))?$file:null;

                    /**
                    * Array param required for url
                    **/
                    $page = $p++;
                    $arrRouteDetail = array(
                        'enterprise_id' => $this->enterprise->id,
                        'enterprise_name' => Tomato_Utility_String::removeSign($this->enterprise->company_name_en, '-', true),
                        'slug' => $this->set->slug,
                        'set_id' => $this->set->set_id,
                        'file_id' => $photo->file_id,
                        'pageIndex' => $page,
                        'image_slug' => $photo->slug
                    );

                ?>

                <?php if($image):?><!--begin image-->
                    <?php if($i%5==0):?>
                <div class="subcolumns">
                    <?php endif;?>
                    <div class="col_20 g_image"  style="text-align: center;">
                        <div class="clearfix b_bottom" style="height: 100px; margin-bottom: 0px;">
                            <a href="<?php echo $this->url($arrRouteDetail, 'b2b_gallery_detail')?>"><img width="120" src="<?php echo $image;?>" /></a>
                        </div>
						<div class="clearfix b_bottom" style="margin-bottom: 0px;">
                            <!-- Mark image -->
                            <?php 
			                	$arrRouteMark = array(
			                		'enterprise_id' => $this->enterprise->id,
			                        'enterprise_name' => Tomato_Utility_String::removeSign($this->enterprise->company_name_en, '-', true),
			                        'slug' => $this->set->slug,
			                        'set_id' => $this->set->set_id,
			                        'file_id' => $photo->file_id
			                    );
			                ?>
			                <!--<?php if($this->allow_mark==1):?>
	                		<?php if($this->checkmark[$i]==0){?>
		                    	<?php  if ($this->allow('mark')) : ?>
									<div id="Mark">
		                    			<a class="markAction" rel="<?php echo $photo->file_id ;?>" href="javascript: void(0);">Mark</a>
		                    		</div>
		                    	<?php  endif;?>
		                    <?php }
		                    	  if($this->checkmark[$i]==1){?>
		                    		<div id="Unmark">
						        	<a href="<?php echo $this->url($arrRouteMark, 'b2b_gallery_unmark');?>">Unmark</a>
						        	</div>
					        <?php }?>
		                	<?php endif;?>-->
		                	
		                	<?php if($this->allow_mark==1):?>                		
			                    	<?php  if ($this->allow('mark')) : ?>
										<div id="Mark">
											<?php if($this->checkmark[$i]==0):?>
			                    			<a class="markAction" val="mark" rel="<?php echo $photo->file_id ;?>" href="javascript: void(0);">
			                    				<span id="actionLabel_<?php echo $photo->file_id;?>"><?php echo $this->translator('gallery_mark_image');?></span>
			                    			</a>
			                    			<?php else:?>
			                    			<a class="markAction" val="unmark" rel="<?php echo $photo->file_id ;?>" href="javascript: void(0);">
			                    				<span id="actionLabel_<?php echo $photo->file_id;?>"><?php echo $this->translator('gallery_unmark_image');?></span>
			                    			</a>
			                    			<?php endif;?>
			                    		</div>
			                    	<?php endif;?>
		                	<?php endif;?>
		                	<!--- End Mark inage-->
                        </div>
                        
                    </div>
                    <?php $i++; if($i%5 == 0):?>
                </div>
                    <?php endif;?>
                <?php endif;?><!--end image-->

                <?php endforeach;?><!--end foreach photo-->

                    <?php if($i%5 != 0):?>
                </div>
                    <?php endif;?>
            </div>
            <div class="b_a_right">
                &nbsp;<?php echo $this->paginator()->slide($this->paginator, $this->paginatorOptions);?>
            </div>
            <?php endif;?>
        </div>
        
       

        <div id="messageDialog"></div>
        <div id="confirmDialog"></div>

        <script type="text/javascript" src="<?php echo $this->APP_STATIC_SERVER; ?>/js/jquery.form/jquery.form.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {

                $('.btn_edit').click(function(){
                    $('#hd_form').show();
                    $('#div_unform').hide();
                });
                $('#btn_unedit').click(function(){
                    set_title = $('#set_title').val();
                    $('#lb_title').text(set_title);

                    $('#hd_form').hide();
                    $('#div_unform').show();
                });

                /**
                * Ajax submit form
                */
                var options = {
                    target: '#outPut', // target element(s) to be updated with server response
                    beforeSubmit:  showRequest,
                    url: '<?php echo $this->url(array(), 'b2b_gallery_album_edit');?>',
                    type: 'POST',  // pre-submit callback,
                    success: function(){
                    }
                };
                $('#set_title').blur(function(){
                    set_title = $('#set_title').val();
                    set_title = set_title.replace(/^\s+|\s+$/g, '');
                    if(set_title != ''){
                        $("#formEditAlbum").ajaxSubmit(options);

                        /**
                        * Update action
                        */
                        set_title = $('#set_title').val();
                        $('#lb_title').text(set_title);
                        $('#hd_form').hide();
                        $('#div_unform').show();
                    }else{
                        return false;
                    }
                });

                $('#set_title').keypress(function(e){
                    if(e.which == 13){
                        set_title = $('#set_title').val();
                        set_title = set_title.replace(/^\s+|\s+$/g, '');
                        if(set_title != ''){
                            $("#formEditAlbum").ajaxSubmit(options);

                            /**
                            * Update action
                            */
                            set_title = $('#set_title').val();
                            $('#lb_title').text(set_title);
                            $('#hd_form').hide();
                            $('#div_unform').show();
                        }else{
                            return false;
                        }
                    }
                });

                /**
                * Delete album
                */
               $('#btn_del_album').click(function(){
                    var self = this;
                    $('#confirmDialog').html('<?php echo addslashes($this->translator('gallery_view_action_delete_album_confirm')); ?>')
                        .dialog({
                            title: '<?php echo addslashes($this->translator('common_dialog_confirm')); ?>',
                            dialogClass: 't_a_ui_dialog',
                            modal: true,
                            closeOnEscape: false,
                            resizable: false,
                            open: function(event, ui) {
                                $('.ui-dialog-titlebar-close').hide();
                                $(this).parent().find('.ui-dialog-buttonpane button').wrapInner('<span/>');
                            },
                            buttons: {
                                '<?php echo addslashes($this->translator('common_dialog_confirm_no')); ?>': function() {
                                    $(this).dialog('destroy');
                                },
                                '<?php echo addslashes($this->translator('common_dialog_confirm_yes')); ?>': function() {
                                    $(this).dialog('destroy');
                                    window.location = '<?php echo $this->url(array('enterprise_id'=>$this->enterprise->id, 'set_id'=>$this->set->set_id), 'b2b_gallery_delete_album');?>';
                                }
                            }
                    });
               });
               /**End delete album*/
               
               /* MArk image*/
                    <?php if ($this->allow('mark')) : ?>
                    $('a.markAction').click(function() {
						var self = this;
						
						val = $(self).attr('val');
						
						//$(this).dialog('destroy');
						//$(self).html('').addClass('t_a_ui_helper_loading');
						var file_id = $(self).attr('rel');
										
						$.ajaxq('b2b', 
						{
							type: 'POST',
							url: "<?php echo $this->url(array(), 'b2b_gallery_mark'); ?>",
							data: { file_id: file_id , type: val},
							success: function(response) 
							{	
								if ('RESULT_OK' == response) 
								{
									//$(self).removeClass('t_a_ui_helper_loading');
									//$('#Mark').fadeOut('slow');
									//$('#Unmark').fadeIn('slow');
									
									/**
									* Update type
									*/
									if(val == 'mark'){
										$('#actionLabel_' + file_id).text("<?php echo $this->translator('gallery_unmark_image');?>");
										$(self).attr('val', 'unmark');
									}else{
										$('#actionLabel_' + file_id).text("<?php echo $this->translator('gallery_mark_image');?>");
										$(self).attr('val', 'mark');
									}
								}
							}
						});
					});
					<?php endif;?>
					/*End mark image*/

            })

            function showRequest(formData, jqForm, options) {
                var queryString = $.param(formData);
                return true;
            }
        </script>

        <?php endif;?>
    </div>
    <div class="grid_4 omega">
        <?php echo $this->widget('bizprofile','map',array()); ?>
        <?php echo $this->widget('bizprofile','help',array()); ?>
    </div>
</div>
<div class="grid_12">
    <?php echo $this->widget('bizprofile','contact',array()); ?>
</div>